package com.cinema.service;

import com.cinema.dao.MovieDao;
import com.cinema.dao.ScheduleDao;
import com.cinema.dao.impl.MovieDaoImpl;
import com.cinema.dao.impl.ScheduleDaoImpl;
import com.cinema.model.Movie;
import com.cinema.model.Schedule;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * 电影服务类
 * 处理电影相关的业务逻辑
 */
public class MovieService {
    private final MovieDao movieDao;
    private final ScheduleDao scheduleDao;

    public MovieService() {
        this.movieDao = new MovieDaoImpl();
        this.scheduleDao = new ScheduleDaoImpl();
    }

    /**
     * 添加电影
     */
    public boolean addMovie(String title, String director, String actors, String plot, int duration, double rating) {
        if (title == null || title.trim().isEmpty()) {
            return false;
        }
        
        String movieId = UUID.randomUUID().toString();
        Movie movie = new Movie(movieId, title.trim(), director.trim(), actors.trim(), plot.trim(), duration, rating);
        return movieDao.save(movie);
    }

    /**
     * 更新电影信息
     */
    public boolean updateMovie(String movieId, String title, String director, String actors, String plot, int duration, double rating) {
        Optional<Movie> movieOpt = movieDao.findById(movieId);
        if (movieOpt.isPresent()) {
            Movie movie = movieOpt.get();
            movie.setTitle(title.trim());
            movie.setDirector(director.trim());
            movie.setActors(actors.trim());
            movie.setPlot(plot.trim());
            movie.setDuration(duration);
            movie.setRating(rating);
            return movieDao.update(movie);
        }
        return false;
    }

    /**
     * 删除电影
     */
    public boolean deleteMovie(String movieId) {
        // 检查是否有排片
        List<Schedule> schedules = scheduleDao.findByMovieId(movieId);
        if (!schedules.isEmpty()) {
            return false; // 有排片的电影不能删除
        }
        
        return movieDao.deleteById(movieId);
    }

    /**
     * 根据ID查找电影
     */
    public Optional<Movie> findMovieById(String movieId) {
        return movieDao.findById(movieId);
    }

    /**
     * 根据电影名查找电影
     */
    public Optional<Movie> findMovieByTitle(String title) {
        return movieDao.findByTitle(title);
    }

    /**
     * 获取所有电影
     */
    public List<Movie> getAllMovies() {
        return movieDao.findAll();
    }

    /**
     * 获取所有正在上映的电影
     */
    public List<Movie> getAllShowingMovies() {
        return movieDao.findAllShowing();
    }

    /**
     * 根据导演查找电影
     */
    public List<Movie> findMoviesByDirector(String director) {
        return movieDao.findByDirector(director);
    }

    /**
     * 根据主演查找电影
     */
    public List<Movie> findMoviesByActor(String actor) {
        return movieDao.findByActor(actor);
    }

    /**
     * 组合查询电影
     */
    public List<Movie> searchMovies(String title, String director, String actor) {
        return movieDao.findByTitleDirectorActor(title, director, actor);
    }

    /**
     * 设置电影上映状态
     */
    public boolean setMovieShowingStatus(String movieId, boolean isShowing) {
        Optional<Movie> movieOpt = movieDao.findById(movieId);
        if (movieOpt.isPresent()) {
            Movie movie = movieOpt.get();
            movie.setShowing(isShowing);
            return movieDao.update(movie);
        }
        return false;
    }

    /**
     * 验证评分范围
     */
    public boolean isValidRating(double rating) {
        return rating >= 0.0 && rating <= 10.0;
    }

    /**
     * 验证时长
     */
    public boolean isValidDuration(int duration) {
        return duration > 0 && duration <= 300; // 最长5小时
    }

    /**
     * 检查电影是否存在
     */
    public boolean existsById(String movieId) {
        return movieDao.existsById(movieId);
    }
}